'use strict';

angular.module('SystemManage').controller('AppRoleListController', ['$scope', 'PublicModule', 'PublicFunction', 'PublicEnum', '$location', 'Authentication',
    function($scope, PublicModule, PublicFunction, PublicEnum, $location, Authentication) {
    	PublicModule.initModule($scope, 'AppRole');
        $scope.Module.AppRole.PageSize = 14;
         $scope.Module.AppRole.Condition = {IsEffective: true};

        $scope.btnAdd = function(){
        	$location.url('/index/AppRole/Edit');
        };

        $scope.rowDblClick = function(){
            $scope.$apply(function(){
                $location.url("/index/AppRole/Edit/" + $scope.Module.AppRole.FocusedRecord._id);
            });
        };

        $scope.btnDelete = function(){
        	$scope.Module.AppRole.deleteSelecteds();
        };

        //下面代码是实现批量添加角色功能
        $scope.onBeforeShow = function(){
            var selectAppRoles = $scope.Module.AppRole.SelectedRecords;
            if(selectAppRoles.length > 0){
                var roleIds = '[';
                for(var i=0; i<selectAppRoles.length; i++){
                    if(i == 0){
                        roleIds += '"' + selectAppRoles[i]._id + '"';
                    }
                    else{
                        roleIds += ',"' + selectAppRoles[i]._id + '"';
                    }
                }
                roleIds += ']';
            
                $scope.Module.User.Condition = {        //不包含所选角色其中任意一个值
                    $where: 'function(){' +
                        'var roles = ' + roleIds + ';' + 
                        'for(var i=0; i<roles.length; i++){' +
                            'if(this.AppRoles.toString().indexOf(roles[i]) < 0) {' +
                                'return true;' +
                            '}' +
                        '}' +
                        'return false;' +
                    '}'
                };

                
                $scope.Module.User.load();
            }
            else{
                PublicFunction.alert("请选择角色后再进行分配");
                return false;
            }
        };

        $scope.btnBatchOK = function(){
            var selectRoles = $scope.Module.AppRole.SelectedRecords;
            var selectUsers = $scope.Module.User.SelectedRecords;
            if(selectUsers.length > 0){
                for(var i=0; i<selectUsers.length; i++) {
                    for(var j=0; j<selectRoles.length; j++){
                        if(selectUsers[i].AppRoles.indexOf(selectRoles[j]._id) < 0){
                            selectUsers[i].AppRoles.push(selectRoles[j]._id);
                            selectUsers[i].RowStatus = PublicEnum.RowStatus.Modify;
                        }
                    }
                }

                $scope.Module.User.save();
            }
            else{
                PublicFunction.alert("请选择用户后再分配");
                return false;
            }
        };

        PublicModule.initModule($scope, 'User');
        $scope.Module.User.Populate = {path: 'Department', select: 'Name'};
        $scope.Module.User.PageSize = 13;
    }
]);
